package utils;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class MessageUtil {

    /**
     * 解析微信发来的请求（XML）
     *
     * @param request
     * @return
     * @throws Exception
     */
    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        // 将解析结果存储在HashMap中  
        Map<String, String> map = new HashMap<String, String>();

        // 从request中取得输入流  
        InputStream inputStream = request.getInputStream();
        // 读取输入流  
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml根元素  
        Element root = document.getRootElement();
        // 得到根元素的所有子节点  
        List<Element> elementList = root.elements();

        // 遍历所有子节点  
        for (Element e : elementList) {
            map.put(e.getName(), e.getText());
        }

        // 释放资源  
        inputStream.close();
        return map;
    }

    public static String getBackXMLTypeText(String toName, String fromName, String content) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String times = format.format(new Date());
        Document doc = DocumentHelper.createDocument();
        Element rootXML = doc.addElement("xml");
        rootXML.addElement("ToUserName").setText(fromName);
        rootXML.addElement("FromUserName").setText(toName);
        rootXML.addElement("CreateTime").setText(times);
        rootXML.addElement("MsgType").setText("text");
        rootXML.addElement("Content").setText(content);
        return rootXML.asXML();
    }

}
